package com.atguigu.gmall.product.cron;

import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.redisson.BloomTask;
import com.atguigu.gmall.redisson.SkuBloomTask;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author 卡尼奶
 * @Date 2022/5/24 18:39
 * @Version 1.0
 */
@Slf4j
@Component
public class SkuBloomTaskCron implements SkuBloomTask {

    @Autowired
    private SkuInfoService skuInfoService;


    @Qualifier("bloomFilter")
    @Autowired
    private RBloomFilter<Object> bloomFilter;

    @Scheduled(cron = "0 0 3 * * 3")
    public void rebuildbloom(){
        //重建布隆
        log.info("系统正在重建布隆...");
        bloomFilter.delete();
        bloomFilter.tryInit(5000000,0.0000001);
        initData(bloomFilter);
    }

    /**
     * 初始化布隆
     */
    @Override
    public void initData(RBloomFilter<Object> bloomFilter) {
        log.info("系统正在初始化布隆...");
        //获取所有sku ID
        List<Long> list = skuInfoService.getAllSkuId();
        //放入布隆过滤器中
        for (Long aLong : list) {
            bloomFilter.add(aLong);
        }
    }
}
